<template>
  <div class="packageDetails">
    <top-bar :title="'礼包详情'"/>
    <div class="header">
      <img :src="giftDetail.game_icon? giftDetail.game_icon: require('../../assets/images/uc_libao.png')" alt="">
      <div>
        <span>{{ giftDetail.title }}</span>
        <span>{{ giftDetail.gift_code }}</span>
      </div>
      <button v-if="giftDetail.gift_code == null" class="ul_btn" @click.stop="addGift(giftDetail)">领取</button>
      <button v-else v-clipboard:copy="giftDetail.gift_code"
              v-clipboard:success="onCopy"
              v-clipboard:error="onError">复制
      </button>
    </div>
    <div class="content">
      <div>
        <span>礼包内容</span>
        <span style="white-space: pre-wrap;">{{ packageContent }}</span>
      </div>
      <div>
        <span>领取条件</span>
        <span v-if="giftDetail.gift_type == 4">累计充值需要达到{{ giftDetail.condition }}元可领</span>
        <span v-if="giftDetail.gift_type == 1">无</span>
      </div>
      <div>
        <span>使用方法</span>
        <span style="white-space: pre-wrap;">{{ usageMethod }}</span>
      </div>
      <div>
        <span>有效时间</span>
        <span>{{ this.dateTransform(giftDetail.start_time) }} ~ {{ this.dateTransform(giftDetail.end_time) }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import topBar from './topBar'

export default {
  name: 'packageDetails',
  components: {
    topBar
  },
  computed: {
    ...mapGetters([
      'giftDetail',
      'actionStatus',
      'failMsg'
    ])
  },
  data () {
    return {
      giftBox: 'pk4342222545',
      packageContent: '',
      usageMethod: '',
      action: ''
    }
  },
  methods: {
    /***
     * 领取礼包
     * */
    addGift (item) {
      this.action = 'add'
      this.$store.dispatch('addGift', item.gift_id)
    },
    // 礼包码复制
    onCopy () {
      this.Toast('复制成功', 500)
    },
    onError () {
      this.Toast('复制失败', 500)
    },
  },
  created () {
    let {id} = this.$route.params
    this.$store.dispatch('getGiftDetail', id)
    setTimeout(() => {
      if (this.giftDetail.content != undefined) {
        this.packageContent = this.giftDetail.content
      }
      if (this.giftDetail.func != undefined) {
        this.usageMethod = this.giftDetail.func
      }
    }, 500)
  },
  watch: {
    actionStatus () {
      if (this.actionStatus) {
        if (this.action === 'add') {
          this.Toast('领取成功', 500)
          this.action = ''
          setTimeout(() => {
            let id = this.giftDetail.gift_id
            this.$router.push(`/me/gamePackage`)
          }, 600)
        }
      }
    },
    failMsg () {
      this.failMsg && this.Toast(this.failMsg, 300)
    }
  }
}
</script>

<style lang="scss" scoped>
.packageDetails {
  padding: 0.75rem 0.48rem;

  .header {
    height: 2rem;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 0.27rem;
    display: flex;
    align-items: center;
    padding: 0.4rem 0.37rem;
    margin-bottom: 0.32rem;

    > img {
      width: 1.18rem;
      height: 1.18rem;
      border-radius: 0.29rem;
      margin-right: 0.35rem;
    }

    > div {
      display: flex;
      flex-direction: column;
      flex: 1;

      > span:nth-child(1) {
        font-size: 0.37rem;
        font-weight: bold;
        color: #181818;
        margin-bottom: 0.16rem;
      }

      > span:nth-child(2) {
        font-size: 0.29rem;
        font-weight: 400;
        color: #FF0300;
      }
    }

    > button {
      width: 1.49rem;
      height: 0.64rem;
      background: #FFD200;
      border-radius: 0.33rem;
      font-size: 0.37rem;
      font-weight: 400;
      color: #000000;
    }
  }

  .content {
    background: rgba(255, 255, 255, 0.6);
    border-radius: 0.27rem;
    padding: 0.53rem 0.37rem;
    height: 6.11rem;
    overflow: scroll;

    > div {
      display: flex;
      flex-direction: column;

      > span:nth-child(1) {
        font-size: 0.32rem;
        font-weight: bold;
        color: #181818;
        margin-bottom: 0.15rem;
      }

      > span:nth-child(2) {
        font-size: 0.27rem;
        font-weight: 400;
        color: #888888;
        line-height: 0.59rem;
        margin-bottom: 0.53rem;
      }
    }
  }
}
</style>
